package org.boot.ssm.wxshop.web.controller;


import com.github.pagehelper.PageInfo;
import org.boot.ssm.wxshop.domain.User;
import org.boot.ssm.wxshop.exception.BusinessException;
import org.boot.ssm.wxshop.service.UserLevelService;
import org.boot.ssm.wxshop.service.UserService;
import org.boot.ssm.wxshop.util.Md5Utils;
import org.boot.ssm.wxshop.util.PhoneMsgCode;
import org.boot.ssm.wxshop.web.common.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

/**
 * 用户控制器类
 *
 * @author 刘涛
 * @date 2018/03/13
 */

@Controller
@RequestMapping(value = "/user")
public class UserController {
    @Autowired
    private UserService userService;
    @Autowired
    private UserLevelService userLevelService;

    /*****************************
     * @Description: 去到用户管理界面
     * @Param: []
     * @return: java.lang.String
     * @Author: 刘涛
     * @Date: 2018/3/14
     *******************************/
    @RequestMapping(value = "/goUserPage")
    public String goUserPage() {
        return "backstage/user/userlist";
    }

    /*****************************
     * @Description: 分页显示用户信息
     * @Param: [pageNum, pageSize, user]
     * @return: java.util.Map<java.lang.String                                                                                                                               ,                                                                                                                               java.lang.Object>
     * @Author: 刘涛
     * @Date: 2018/3/14
     *******************************/
    @RequestMapping(value = "/list")
    @ResponseBody
    public Map<String, Object> listUser(@RequestParam(value = "pageNum", required = false, defaultValue = "1") Integer pageNum, @RequestParam(value = "pageSize", required = false, defaultValue = "10") Integer pageSize, User user) {
        user.setIsDelete(1);
        Map<String, Object> json = new HashMap<>();
        PageInfo<User> page = userService.queryPageListByParam(pageNum, pageSize, user);
        json.put("code", 0);
        json.put("msg", "加载成功");
        json.put("count", page.getTotal());
        json.put("data", page.getList());
        return json;
    }


    /*****************************
     * @Description: 去到后台添加用户页面
     * @Param: []
     * @return: java.lang.String
     * @Author: 刘涛
     * @Date: 2018/3/14
     *******************************/
    @RequestMapping(value = "/goAddUserPage")
    public String goAddUserPage() {
        return "backstage/user/adduser";
    }


    /*****************************
     * @Description:用户登录名验证
     * @Param: [user]
     * @return: org.boot.ssm.wxshop.web.common.JsonResult
     * @Author: 刘涛
     * @Date: 2018/3/14
     *******************************/
    @RequestMapping(value = "/verifyLoginName")
    @ResponseBody
    public JsonResult verifyLoginName(User user) {

        return userService.verifyLoginName(user);
    }

    /*****************************
     * @Description:用户登录名验证
     * @Param: [user]
     * @return: org.boot.ssm.wxshop.web.common.JsonResult
     * @Author: 刘涛
     * @Date: 2018/3/14
     *******************************/
    @RequestMapping(value = "/verifyLoginPwd")
    @ResponseBody
    public JsonResult verifyLoginPwd(User user) {
        user.setLoginpwd(Md5Utils.getMd5(user.getLoginpwd()));
        return userService.verifyLoginName(user);
    }


    /*****************************
     * @Description: 后台管理界面添加或修改用户
     * @Param: [user]
     * @return: org.boot.ssm.wxshop.web.common.JsonResult<java.lang.String>
     * @Author: 刘涛
     * @Date: 2018/3/14
     *******************************/
    @RequestMapping(value = "/saveOrUpdateUserInBack")
    @ResponseBody
    public JsonResult saveOrUpdateUserInBack(User user) {
        return userService.addOrUpdateUser(user);
    }


    /*****************************
     * @Description: 去到后台修改用户页面 ，并显示用户数据
     * @Param: [id, model]
     * @return: java.lang.String
     * @Author: 刘涛
     * @Date: 2018/3/14
     *******************************/
    @RequestMapping(value = "/goUpdateUserPage/{id}")
    public String goUpdateUserPage(@PathVariable(name = "id", required = true) Integer id, Model model) {
        model.addAttribute("user", userService.queryById(id));
        return "backstage/user/updateuser";
    }

    /*****************************
     * @Description: 逻辑删除用户
     * @Param: [user]
     * @return: org.boot.ssm.wxshop.web.common.JsonResult
     * @Author: 刘涛
     * @Date: 2018/3/14
     *******************************/
    @RequestMapping(value = "/deleteUser")
    @ResponseBody
    public JsonResult deleteUser(User user) {
        return userService.deleteUser(user);
    }


    /*****************************
     * @Description:去到设置用户等级页面
     * @Param: [id]
     * @return: java.lang.String
     * @Author: 刘涛
     * @Date: 2018/3/14
     *******************************/
    @RequestMapping(value = "/gosetLevelPage/{id}")
    public String gosetLevelPage(@PathVariable(value = "id") Integer id, Model model) {
        model.addAttribute("user", userService.selectUserAndLevel(id));
        model.addAttribute("list", userLevelService.queryAllList());
        return "backstage/user/setLevel";
    }


    /*****************************
     * @Description: 更改用户等级
     * @Param: [user]
     * @return: org.boot.ssm.wxshop.web.common.JsonResult
     * @Author: 刘涛
     * @Date: 2018/3/14
     *******************************/
    @RequestMapping(value = "/updateUserLevel")
    @ResponseBody
    public JsonResult updateUserLevel(User user) {
        return userService.addOrUpdateUser(user);
    }


    /**
     * 去到重置密码页面
     *
     * @param id
     * @param model
     * @return
     */
    @RequestMapping(value = "/resetPwdPage/{id}")
    public String resetPwdPage(@PathVariable(name = "id") Integer id, Model model) {
        model.addAttribute("id", id);
        return "backstage/user/resetpwd";
    }


    /*****************************
     * @Description: 重置密码
     * @Param: [user]
     * @return: org.boot.ssm.wxshop.web.common.JsonResult
     * @Author: 刘涛
     * @Date: 2018/3/18
     *******************************/
    @RequestMapping(value = "/resetPwd")
    @ResponseBody
    public JsonResult resetPwd(User user) {
        return userService.resetPwd(user);
    }

    /**
     * 去到用户中心
     *
     * @return
     */
    @RequestMapping(value = "/frontstage/goUserCenterPage")
    public String goUserCenterPage() {
        return "frontstage/user/userCenter";
    }

    /**
     * 去到前台修改密码页面
     *
     * @return
     */
    @RequestMapping(value = "/frontstage/editPwd")
    public String editPwd() {
        return "frontstage/user/editPwd";
    }

    /**
     * 去注册页面
     *
     * @return
     */
    @RequestMapping(value = "/frontstage/goRegistPage")
    public String goRegistPage() {
        return "frontstage/user/regist";
    }

    /**
     * 获取短信验证码
     *
     * @param phone
     * @return
     */
    @RequestMapping(value = "/frontstage/getDynamicNum")
    @ResponseBody
    public JsonResult getDynamicNum(String phone) {
        try {
            // System.out.println(phone);
            return PhoneMsgCode.getOutIdAndBizId(phone);
        } catch (Exception e) {
            throw new BusinessException(e.getMessage());
        }

    }

    /**
     * 注册用户
     *
     * @param user
     * @param session
     * @return
     */
    @RequestMapping(value = "/frontstage/registUser")
    @ResponseBody
    public JsonResult registUser(User user, HttpSession session) {
        JsonResult jsonResult = userService.registUser(user);
        session.setAttribute("user", jsonResult.getData());
        return jsonResult;
    }


    /**
     * 修改用户密码
     * @param oldPwd
     * @param newPwd
     * @param session
     * @return
     */
    @RequestMapping(value = "/frontstage/editUserPwd")
    @ResponseBody
    public JsonResult editUserPwd(String oldPwd, String newPwd, HttpSession session) {
        JsonResult jsonResul = userService.editPwd((User) session.getAttribute("user"), Md5Utils.getMd5(oldPwd), Md5Utils.getMd5(newPwd));
        session.removeAttribute("user");
        return jsonResul;
    }

}
